package com.ts.proj.service.impl;

import com.ts.proj.entity.FinanceEntity;
import com.ts.proj.repository.FinanceRepository;
import com.ts.proj.service.FinanceService;
import com.ts.util.EntityAttributeUtil;
import com.ts.util.PageData;

import com.ts.base.service.impl.CommonServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


//说明 字符串：，作为替换字符串 LOWERFinance                Finance

@Service
public class FinanceServiceImpl extends CommonServiceImpl<FinanceEntity>  implements FinanceService {

    @Autowired
    private FinanceRepository financeRepository;

    @Override
    public List<FinanceEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<FinanceEntity> sp = this.getSpecification(pageData,FinanceEntity.class);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return financeRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count(PageData pageData) {
        Specification<FinanceEntity> sp = this.getSpecification(pageData,FinanceEntity.class);
        return financeRepository.count(sp);
    }

    @Override
    public boolean deleteByIds(Iterable<FinanceEntity> iterable) {
        financeRepository.deleteInBatch(iterable);
        return false;
    }

    @Override
    public List<FinanceEntity> saveAll(List<FinanceEntity> entities) {
        return financeRepository.saveAll(entities);
    }

    @Override
    public List<FinanceEntity> findAllById(Integer[] ids) {
        List<FinanceEntity> list = financeRepository.findAllById(Arrays.asList(ids));
        if(list.size()==0){
            FinanceEntity entity = new FinanceEntity();
            list.add(entity);
        }
        return list;
    }

    @Override
    public boolean deleteAll() {
		financeRepository.deleteAll();
        return false;
    }

    @Override
    public Specification getSpecification(PageData pageData, Class entityClass) {
        return new Specification() {
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList();
                List<String> fieldList = EntityAttributeUtil.entityAttribute(entityClass);

                if(null!=pageData.get("pro_Id")&&!"".equals(pageData.getString("pro_Id"))){
                    preList.add(criteriaBuilder.equal(root.get("proId"), pageData.get("pro_Id").toString()));
                }


                preList.add(criteriaBuilder.equal(root.get("deleteFlag"),"N"));
                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and((Predicate[])preList.toArray(p));
            }
        };
    }

    @Override
    public FinanceEntity findByProId(Integer id) {
        return financeRepository.findByProId(id);
    }

    @Override
    public List<PageData> findProjectAndFinanceList(String name) {
        return financeRepository.findProjectAndFinanceList(name);
    }
}